home *** CD-ROM | disk | FTP | other *** search
- /* init to disable power mgr idling on plugged-in-ness of charger
-
- © 1992 by Stephan Somogyi, all rights reserved
-
- The source code below may not be used in a commercial product without the written consent
- of the author. It may, however, be used in not-for-profit software, provided that
- adequate attribution of its origin is given.
-
- This source code is freely distributable if unaltered.
-
-
- 06.06.92 0.1.0 MacPCP
- 09.06.92 0.1.1 various source cleanings up
- renamed to Insomnia
- 13.06.92 0.2.0 added use of deferred task mgr per dts' suggetion
- compiled with MPW to check for sillinesses that think won't catch
- 14.06.92 0.3 added separate code & icons for DetachResource and VInstall errs
- built version with DEBUG support
- 15.06.92 0.3.1 added StripAddresses everywhere. dunno if it'll help. sigh.
- 17.06.92 0.4 added SaveTheWorld/RestoreTheWorld
- 0.4.1 added lotsa comments and cleaned up a bit
- 18.06.92 0.4.2 removed StripAddresses again because they seem somewhat pointless
- 0.4.3 added zeroing of all unused fields in the DeferredTaskBlock in
- every vbl
- 1.0a1 released at MacHack.
- the 1st person who finds the bug and tells me gets a t-shirt.
-
-
- my weird source code conventions: any identifier beginning with a
-
- k is a constant
- g is a global var
- l is a local var
-
- */
-
-
- /* the #ifndef below speeds up think compiles since the .h files in question are in macheaders.
- without it, however, the c part of this won't compile under mpw. the down-side of this is no
- automatic access to the .h files via the title-bar in the think environment.
- */
-
- #ifndef THINK_C
- #include <Traps.h>
- #include <GestaltEqu.h>
- #include <Resources.h>
- #include <OSUtils.h>
- #include <Memory.h>
- #endif
-
- #include <SetupA4.h> // these aren't in macheaders
- #include <Power.h>
- #include <Retrace.h>
-
-
- // the purpose of the following is to provide an easy way to save and restore the world.
- // it was added after not being able to hunt down the weird GetWTitle crash. it may be
- // overkill.
-
- #define kSaveRegList a0-a5/d0-d7
- #define SaveTheWorld asm { movem.l kSaveRegList,-(sp) }
- #define RestoreTheWorld asm { movem.l (sp)+,kSaveRegList }
-
-
- // this isn't defined in any .h file that I could find
-
- typedef struct DeferredTask {
- QElemPtr qLink;
- short qType;
- short dtFlags;
- ProcPtr dtAddr;
- long dtParm;
- long dtReserved;
- } DeferredTask;
-
-
- #define kNotApplicableIcon 131 // icon displayed if the mac is an inappropriate one
- #define kErrVblInstallIcon 130 // vinstall failure at init time
- #define kErrRsrcInstallIcon 129 // detatchresource failure at init time
- #define kOKInstallIcon 128 // installed fine
-
- #define kVBLInterval 10*60 // # seconds * ticks between vbl executions
-
-
- // prototypes
- pascal void SHOWINIT(short iconID, short moveX);
- pascal void PCPVBL(void);
- pascal void PCPDTask(void);
-
-
- // globals - think c provides the ability to do globals in code resources by referencing them
- // off a4. all you have to do is stash the beginning of the code rsrc away at init time, set up
- // a4, and then restore it again when you're done.
- // think provides pre-zeroed space at the end of the code rsrc for the globals - no additional
- // allocation or anything else is necessary.
-
- VBLTask gVTaskBlk;
- DeferredTask gDTaskBlk;
- Boolean gDidIDisableIdle; // must make sure DisableIdles and EnableIdles are balanced
-
-
- void
- main()
- {
- Ptr lOurAddress;
- long lGestaltResponse;
- OSErr lErr;
-
-
- RememberA0(); // glue from think's setupa4.h which stashes away
- // a pointer to the beginning of the rsrc for
- // later use.
-
- SetUpA4(); // sets up a4 for globals.
-
- asm {
- move.l A0, lOurAddress ; A0 points to the beginning of the
- ; INIT, courtesy of the THINK glue
- }
-
- lErr = Gestalt(gestaltPowerMgrAttr, &lGestaltResponse);
- if (lErr != noErr ||
- lGestaltResponse == gestaltUnknownErr ||
- lGestaltResponse == gestaltUndefSelectorErr) {
- SHOWINIT(kNotApplicableIcon, -1);
- RestoreA4();
- return;
- }
- if (!(lGestaltResponse & 1)) { // PMgrExists - is there a power mgr?
- SHOWINIT(kNotApplicableIcon, -1);
- RestoreA4();
- return;
- }
- if (!(lGestaltResponse & 2)) { // PMgrCPUIdle - is the cpu capable of idling?
- SHOWINIT(kNotApplicableIcon, -1);
- RestoreA4();
- return;
- }
-
- DetachResource(RecoverHandle(lOurAddress)); // make myself permanent only if it's sensible
- if (ResError() != noErr) {
- SHOWINIT(kErrRsrcInstallIcon, -1);
- RestoreA4();
- return;
- }
-
- gVTaskBlk.qType = vType;
- gVTaskBlk.vblAddr = (VBLProcPtr) PCPVBL;
- gVTaskBlk.vblCount = kVBLInterval; // frequency of execution
-
- lErr = VInstall((QElemPtr) &gVTaskBlk); // install my vbl task
- if (lErr != noErr) {
- SHOWINIT(kErrVblInstallIcon, -1);
- RestoreA4();
- return;
- }
-
- SHOWINIT(kOKInstallIcon, -1); // installed ok - show "normal" icon
-
- RestoreA4();
- }
-
-
- pascal void
- PCPVBL()
- {
- OSErr lErr;
-
-
- #ifdef DEBUG
- Debugger();
- #endif
-
- SaveTheWorld // a macro that expands - see above
-
- SetUpA4();
-
- gDTaskBlk.qLink = (QElemPtr) 0L;
- gDTaskBlk.qType = dtQType;
- gDTaskBlk.dtFlags = 0;
- gDTaskBlk.dtAddr = (ProcPtr) PCPDTask;
- gDTaskBlk.dtParm = 0L;
- gDTaskBlk.dtReserved = 0L;
-
- lErr = DTInstall((QElemPtr) &gDTaskBlk); // install a deferred task
-
- #ifdef DEBUG
- if (lErr != noErr)
- DebugStr("\pDTInstall failed");
- #endif
-
- gVTaskBlk.vblCount = kVBLInterval; // frequency of execution of the vbl. need to reset
- // this every time otherwise i won't be executed
- // ever again.
-
- RestoreA4(); // restorea4 and restoretheworld must be in the
- // correct order, otherwise bad things happen
-
- RestoreTheWorld
- }
-
-
- pascal void
- PCPDTask()
- {
- Byte lStatus, lPower;
- OSErr lErr;
-
-
- #ifdef DEBUG
- Debugger();
- #endif
-
- SaveTheWorld
-
- SetUpA4();
-
- lErr = BatteryStatus(&lStatus, &lPower);
-
- #ifdef DEBUG
- if (lErr != noErr)
- DebugStr("\pBatteryStatus failed");
- #endif
-
- if (lStatus & 1) { // charger connected?
- if (!gDidIDisableIdle) {
- DisableIdle();
- gDidIDisableIdle = true;
- }
- }
- else {
- if (gDidIDisableIdle) {
- EnableIdle();
- gDidIDisableIdle = false;
- }
- }
-
- RestoreA4();
-
- RestoreTheWorld
- }
-